package com.dianqu.weather.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dianqu.weather.core.common.Tools;
import com.dianqu.weather.core.model.ClientManager;
import com.dianqu.weather.core.service.ClientService;
import com.dianqu.weather.core.service.WeatherService;

@Controller
@RequestMapping(value = "/query")
public class QueryController {
	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	public WeatherService weatherService;
	@Autowired
	public ClientService clientService;
	@Autowired
	public ClientManager clientManager;

	@ResponseBody
	@RequestMapping(value = "", params = "online", produces = "text/plain;charset=UTF-8")
	public String queryOnline() {
		return "" + clientManager.totalOnlineClient();
	}

	@ResponseBody
	@RequestMapping(value = "", params = "location", produces = "text/plain;charset=UTF-8")
	public String queryWeather(String location, HttpServletRequest request) {
		location = Tools.iso2utf8(location);
		// add log to database
		clientService.addLog(request, location);

		// query weather
		return weatherService.service(location.trim());
	}

}
